class Solution
{
public:
    vector<int> getGoodIndices(vector<vector<int>> &variables, int target)
    {
        vector<int> results;

        auto fastPow = [](int base, int exp, int mod) -> int
        {
            int result = 1;
            base = base % mod;
            while (exp)
            {
                if (exp & 1)
                {
                    result = (result * base) % mod;
                }
                base = (base * base) % mod;
                exp >>= 1;
            }
            return result;
        };

        for (int i = 0; i < variables.size(); i++)
        {
            auto &v = variables[i];
            int a = v[0];
            int b = v[1];
            int c = v[2];
            int m = v[3];

            // ((ai ^bi % 10) ^ ci) % mi

            int result = fastPow(a, b, 10);
            result = fastPow(result, c, m);
            if (result == target)
            {
                results.push_back(i);
            }
        }
        return results;
    }
};